package cn.sibu.myroot.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.LinearLayout;

/**
 * Created by 刘红亮 on 15-12-18 上午11:53.
 */
public class PerspectiveLayout extends LinearLayout{
    public PerspectiveLayout(Context context) {
        super(context);
        init();
    }

    public PerspectiveLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PerspectiveLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        //启用静态变换,这样对于每个子视图都会调用getChildStaticTransformation();
        setStaticTransformationsEnabled(true);
    }

    @Override
    protected boolean getChildStaticTransformation(View child, Transformation t) {

        Log.e("StaticTransformation","getChildStaticTransformation");
        //清除所有现有的变换
        t.clear();

        if(getOrientation() == HORIZONTAL){
            //根据到左边缘的距离对子视图进行缩放
            float delta = 1.0f-((float)child.getLeft()/getWidth());
            t.getMatrix().setScale(delta,delta,child.getWidth()/2,child.getHeight()/2);
        }else {
            //根据到顶端边缘的距离对子视图进行缩放
            float delta = 1.0f-((float)child.getTop()/getHeight());
            t.getMatrix().setScale(delta,delta,child.getWidth()/2,child.getHeight()/2);
            //同样也根据它的位置应用颜色淡出效果
            t.setAlpha(delta);
        }

        return true;
    }
}
